package com.cym.utils;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.cym.config.SqlConfig;
import com.cym.ext.Path;
import com.cym.model.Repository;
import com.cym.service.RepositoryService;

import cn.craccd.sqlHelper.utils.SqlHelper;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.CharsetUtil;
import cn.hutool.core.util.RuntimeUtil;
import cn.hutool.json.JSONUtil;

@Component
public class PathUtls {
	@Autowired
	SqlConfig sqlConfig;
	@Autowired
	SqlHelper sqlHelper;
	@Autowired
	RepositoryService repositoryService;

	public List<Path> testStartUp(String repositoryId) {
		Repository repository = sqlHelper.findById(repositoryId, Repository.class); 

		String home = sqlConfig.home;
		List<String> lines = null;
		if (SystemTool.isWindows()) {

			if (!home.contains(":")) {
				// 获取盘符
				home = JarUtil.getCurrentFilePath().split(":")[0] + ":" + home;
			}
			String cmd = "svnlook.exe tree " + (home + File.separator + "repo" + File.separator + repository.getName() + File.separator).replace("/", "\\");

			FileUtil.writeString(cmd, home + File.separator + "tree.bat", CharsetUtil.GBK);

			lines = RuntimeUtil.execForLines(home + File.separator + "tree.bat");

			lines.remove(0);
			lines.remove(0);
		} else {
			String cmd = "svnlook tree " + (home + File.separator + "repo" + File.separator + repository.getName() + File.separator);

			FileUtil.writeString(cmd, home + File.separator + "tree.sh", CharsetUtil.UTF_8);

			lines = RuntimeUtil.execForLines("sh", home + File.separator + "tree.bat");

			lines.remove(0);
			lines.remove(0);
		}

		List<Path> paths = new ArrayList<>();

		int blankCount = 0;
		while (hasBlankFile(blankCount, lines)) {
			String blank = buildBlank(blankCount);
			for (String line : lines) {
				boolean finded = false;
				if (blank.equals("") && !line.startsWith(" ")) {
					finded = true;
				}

				if (!blank.equals("") && line.startsWith(blank)) {
					finded = true;
				}

				if (finded) {
					Path path = new Path();
					path.setType(line.contains("/") ? 1 : 0);
					path.setId(line);
					path.setName(line.trim());
					path.setPid(findPid(line, lines));
					paths.add(path);
				}
			}

			blankCount++;
		}

		System.out.println(JSONUtil.toJsonPrettyStr(paths));
		return paths;
	}

	

	private String findPid(String line, List<String> lines) {
		if (!line.startsWith(" ")) {
			return null;
		} else {
			int blankCount = getLineBlankCount(line);
			int index = 0;
			for (int i = 0; i < lines.size(); i++) {
				if (lines.get(i).equals(line)) {
					index = i;
					break;
				}
			}

			for (int i = index; i >= 0; i--) {
				if (getLineBlankCount(lines.get(i)) == blankCount - 1) {
					index = i;
					break;
				}
			}

			return lines.get(index);
		}
	}

	private int getLineBlankCount(String line) {
		int blankCount = 0;
		for (int i = 0; i < line.length(); i++) {
			char tmp = line.charAt(i);
			if (tmp == ' ') {
				blankCount++;
			}
		}

		return blankCount;
	}

	private String buildBlank(int blankCount) {
		String blank = "";
		for (int i = 0; i < blankCount; i++) {
			blank += " ";
		}

		return blank;
	}

	private boolean hasBlankFile(int blankCount, List<String> lines) {
		String blank = buildBlank(blankCount);
		for (String line : lines) {
			if (blank.equals("") && !line.startsWith(" ")) {
				return true;
			}

			if (!blank.equals("") && line.startsWith(blank)) {
				return true;
			}
		}
		return false;
	}

}
